package com.craftplay.dao;

import com.craftplay.model.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Delete({
            "delete from payorder",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
            "insert into payorder ( outTradeNo, ",
            "userId, unitId,classType,classTreeType, ",
            "money, createTime, ",
            "status,message)",
            "values ( #{outTradeNo,jdbcType=VARCHAR}, ",
            "#{userId,jdbcType=INTEGER}, #{unitId,jdbcType=INTEGER},#{classType,jdbcType=INTEGER},#{classTreeType,jdbcType=INTEGER}, ",
            "#{money,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{status,jdbcType=INTEGER},#{message,jdbcType=VARCHAR})"
    })
    int insert(Order record);

    @InsertProvider(type = OrderSqlProvider.class, method = "insertSelective")
    int insertSelective(Order record);

    @Select({
            "select",
            "id, outTradeNo, userId, unitId, money, createTime, status, payTime,message",
            "from payorder",
            "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "outTradeNo", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "money", property = "money", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "payTime", property = "payTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "message", property = "message", jdbcType = JdbcType.VARCHAR)
    })
    Order selectByPrimaryKey(Integer id);

    @UpdateProvider(type = OrderSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Order record);

    @Update({
            "update payorder",
            "set outTradeNo = #{outTradeNo,jdbcType=VARCHAR},",
            "userId = #{userId,jdbcType=INTEGER},",
            "unitId = #{unitId,jdbcType=INTEGER},",
            "money = #{money,jdbcType=INTEGER},",
            "createTime = #{createTime,jdbcType=TIMESTAMP},",
            "status = #{status,jdbcType=INTEGER},",
            "payTime = #{payTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Order record);

    @Select({
            "select *",
            "from payorder",
            "where outTradeNo = #{outTradeNo,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "outTradeNo", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "money", property = "money", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "payTime", property = "payTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "message", property = "message", jdbcType = JdbcType.VARCHAR),
            @Result(column = "classType", property = "classType", jdbcType = JdbcType.INTEGER),
            @Result(column = "classTreeType", property = "classTreeType", jdbcType = JdbcType.INTEGER)
    })
    Order selectByOutTradeNo(String outTradeNo);


    @Select({
            "select",
            "id, outTradeNo, userId, unitId, money, createTime, status, payTime",
            "from payorder",
            "where userId = #{userId,jdbcType=INTEGER} and unitId = #{unitId,jdbcType=INTEGER} and status=2"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "outTradeNo", property = "outTradeNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "unitId", property = "unitId", jdbcType = JdbcType.INTEGER),
            @Result(column = "money", property = "money", jdbcType = JdbcType.INTEGER),
            @Result(column = "createTime", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "payTime", property = "payTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Order> isPay(@Param(value = "userId") Integer userId, @Param(value = "unitId") Integer unitId);
}